﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FzWeb.Models
{
    public class PersonService
    {
        public Entities db
        {
            get
            {
                if (HttpContext.Current.Session["db"] == null)
                {
                    HttpContext.Current.Session["db"] = new Entities();
                }
                return HttpContext.Current.Session["db"] as FzWeb.Models.Entities;
            }
        }

        public Person GetPerson(int id)
        {
            return db.Person.Include("user").First(c => c.PersonId == id);
        }

        public IList<Person> GetPersonList()
        {
            return db.Person.Include("user").ToList();
        }

        public Person_Resume GetResume(int id)
        {
            return db.Person_Resume.Include("Person_Work_Experience").
                Include("person").Include("person.user").
                Include("Person_Train").Include("Person_Eduction").
                Include("Person_Skill").Include("Person_WillJob").First(c => c.ResumeId == id);
        }

        public void DelResume(int id)
        {
            db.Person_Resume.First(c => c.ResumeId == id).DelMark = true;
            db.SaveChanges();
        }

        public IList<Person_Resume> GetResumeList(Func<Person_Resume, bool> predict)
        {
            return db.Person_Resume.Include("person").Include("Person_WillJob").Where(predict).OrderByDescending(c => c.RefleshTime).ToList();
        }

        public IList<Person_Resume> GetResumeList()
        {
            return db.Person_Resume.Include("person").Include("Person_WillJob").Include("person.user").OrderByDescending(c => c.RefleshTime).ToList();
        }

        public void UpdatePerson(Person p)
        {
            var oPerson = db.Person.Where(d => d.PersonId == p.PersonId).FirstOrDefault();
            db.ApplyPropertyChanges(oPerson.EntityKey.EntitySetName, p);
            db.SaveChanges();
        }

        public void CreatePerson(Person p)
        {
            db.AddToPerson(p);
            db.SaveChanges();
        }

        public void saveChanges(string EntitySetName, object changed)
        {
            db.ApplyPropertyChanges(EntitySetName, changed);
            db.SaveChanges();
        }

        public void saveChanges(Person p)
        {
            db.ApplyPropertyChanges(GetPerson(p.PersonId).EntityKey.EntitySetName, p);
            db.SaveChanges();
        }

        public void saveChanges()
        {
            db.SaveChanges();
        }


        public Person_Skill getPersonResumeLanguage(int id)
        {
            return db.Person_Skill.First(c => c.SkillId == id && (c.Type == 1));
        }

        /// <summary>
        /// 取当前简历下所有的语言
        /// </summary>
        /// <param name="id">传入参数ID 实际上是对应简历的ID</param>
        /// <returns></returns>
        public IList<Person_Skill> getPersonResumeLanguages(int id)
        {
            return db.Person_Skill.Where(c => c.Person_Resume.ResumeId == id && (c.Type == 1)).ToList();
        }

        public void CreatePersonResumeLanguage(Person_Skill ps)
        {
            db.AddToPerson_Skill(ps);
            db.SaveChanges();
        }

        internal void DeletePersonResumeLanguage(Person_Skill ps)
        {
            db.DeleteObject(ps);
            db.SaveChanges();
        }

        internal void UpdatePersonResumeLanguage(Person_Skill ps)
        {
            var originalps = db.Person_Skill.Where(c => c.SkillId == ps.SkillId && (c.Type == 1)).FirstOrDefault();
            db.ApplyPropertyChanges(originalps.EntityKey.EntitySetName, ps);
            db.SaveChanges();
        }


        internal Person_Eduction GetPersonEducation(int p)
        {
            return db.Person_Eduction.First(c => c.EduId == p);
        }

        internal void CreatePersonEducation(Person_Resume resume, Person_Eduction pe)
        {
            resume.Person_Eduction.Add(pe);
            db.SaveChanges();
        }

        internal void UpdatePersonEducation(Person_Eduction pe)
        {
            db.ApplyPropertyChanges(GetPersonEducation(pe.EduId).EntityKey.EntitySetName, pe);
            db.SaveChanges();
        }

        public Person_Skill getPersonResumeSkill(int id)
        {
            return db.Person_Skill.First(c => c.SkillId == id && (c.Type == 1));
        }

        /// <summary>
        /// 取当前简历下所有的技能
        /// </summary>
        /// <param name="id">传入参数ID 实际上是对应简历的ID</param>
        /// <returns></returns>
        public IList<Person_Skill> getPersonResumeSkills(int id)
        {
            return db.Person_Skill.Where(c => c.Person_Resume.ResumeId == id && (c.Type == 1)).ToList();
        }

        public void CreatePersonResumeSkill(Person_Skill ps)
        {
            db.AddToPerson_Skill(ps);
            db.SaveChanges();
        }

        internal void DeletePersonResumeSkill(Person_Skill ps)
        {
            db.DeleteObject(ps);
            db.SaveChanges();
        }

        internal void UpdatePersonResumeSkill(Person_Skill ps)
        {
            var originalps = db.Person_Skill.Where(c => c.SkillId == ps.SkillId && (c.Type == 1)).FirstOrDefault();
            db.ApplyPropertyChanges(originalps.EntityKey.EntitySetName, ps);
            db.SaveChanges();
        }

        /// <summary>
        /// 个人求职意向
        /// </summary>
        /// <param name="id">传入参数ID 实际上是对应简历的ID</param>
        /// <returns></returns>
        public IList<Person_WillJob> getPersonResumeWillJobs(int id)
        {
            return db.Person_WillJob.Where(c => c.Person_Resume.ResumeId == id).ToList();
        }

        public Person_WillJob getPersonResumeWillJob(int id)
        {
            return db.Person_WillJob.First(c => c.WillJobId == id);
        }


        public void CreatePersonResumeWillJob(Person_WillJob pw)
        {
            db.AddToPerson_WillJob(pw);
            db.SaveChanges();
        }

        internal void DeletePersonResumeWillJob(Person_WillJob pw)
        {
            db.DeleteObject(pw);
            db.SaveChanges();
        }

        internal void UpdatePersonResumeWillJob(Person_WillJob pw)
        {
            var originalpw = db.Person_WillJob.Where(c => c.WillJobId == pw.WillJobId).FirstOrDefault();
            db.ApplyPropertyChanges(originalpw.EntityKey.EntitySetName, pw);
            db.SaveChanges();
        }

        internal Person_Job_History GetPersonJobHistory(int id)
        {
            return db.Person_Job_History.First(c => c.JobHistoryId == id);
        }

        internal void DelPersonJobHstory(int id, int personid)
        {
            db.DeleteObject(db.Person_Job_History.First(c => c.JobHistoryId == id && c.Person.PersonId == personid));
            db.SaveChanges();
        }

        public Person_Job_History GetPersonJobHistory(Func<Person_Job_History, bool> predicate)
        {
            return db.Person_Job_History.First(predicate);
        }

        public IList<Person_Job_History> GetPersonJobHistoryList(Func<Person_Job_History, bool> predict)
        {
            return db.Person_Job_History.Where(predict).OrderBy(c => c.PostTime).ToList();
        }

        public Person_Job_Favorite GetPersonJobFavorite(int id)
        {
            return db.Person_Job_Favorite.First(c => c.JobFavoriteId == id);
        }

        public IList<Person_Job_Favorite> GetPersonJobFavoriteList(Func<Person_Job_Favorite, bool> predict)
        {
            return db.Person_Job_Favorite.Include("post").Include("post.company").Where(predict).OrderBy(c => c.FavoriteTime).ToList();
        }

        public IList<Person_Job_Apply> GetPersonJobApplyList(Func<Person_Job_Apply, bool> predict)
        {
            return db.Person_Job_Apply.Include("person_resume").Include("post").Include("person").Include("post.company").Where(predict).OrderBy(c => c.ApplyTime).ToList();
        }

        public IList<Settlement> GetSettlementList(Func<Settlement, bool> predict)
        {
            return db.Settlement.Include("person").Where(predict).OrderBy(c => c.SettleStartDate).ToList();
        }

        public Settlement GetSettlement(Func<Settlement, bool> predicate)
        {
            return db.Settlement.First(predicate);
        }


        public void CreatePersonJobFavorite(Person_Job_Favorite pjf)
        {
            db.AddToPerson_Job_Favorite(pjf);
            db.SaveChanges();
        }

        public void CreatePersonJobApply(Person_Job_Apply pja)
        {
            db.AddToPerson_Job_Apply(pja);
            db.SaveChanges();
        }

        internal Person_Job_Apply GetPersonJobApply(int id)
        {
            return db.Person_Job_Apply.First(c => c.JobApplyId == id);
        }

        internal void DelPersonJobApply(Person_Job_Apply pja)
        {
            db.DeleteObject(pja);
            db.SaveChanges();
        }
        internal void DelPersonJobApply(int id)
        {
            db.DeleteObject(db.Person_Job_Apply.First(c => c.JobApplyId == id));
            db.SaveChanges();
        }

        internal void DelPersonJobFavorite(Person_Job_Favorite pjf)
        {
            db.DeleteObject(pjf);
            db.SaveChanges();
        }

        internal void DelPersonJobFavorite(int id)
        {
            db.DeleteObject(db.Person_Job_Favorite.First(c => c.JobFavoriteId == id));
            db.SaveChanges();
        }
        /// <summary>
        /// 推荐人才
        /// </summary>
        /// <returns></returns>
        internal IList<Person> GetPersonRecoomend()
        {
            return db.Person.Take(10).ToList();
        }

        internal void UpdatePersonJobHistory(Person_Job_History pjh)
        {
            db.ApplyPropertyChanges(GetPersonJobHistory(pjh.JobHistoryId).EntityKey.EntitySetName, pjh);
            db.SaveChanges();
        }


        internal void UpdatePersonResume(Person_Resume pr)
        {
            db.ApplyPropertyChanges(GetResume(pr.ResumeId).EntityKey.EntitySetName, pr);
            db.SaveChanges();
        }

        internal void DelResumeEducation(int id, int resumeid)
        {
            db.DeleteObject(db.Person_Eduction.First(c => c.EduId == id && c.Person_Resume.ResumeId == resumeid));
            db.SaveChanges();
        }

        internal void DelResumeTrain(int id, int resumeid)
        {
            db.DeleteObject(db.Person_Train.First(c => c.TrainId == id && c.Person_Resume.ResumeId == resumeid));
            db.SaveChanges();
        }

        internal void DelResumeWork(int id, int resumeid)
        {
            db.DeleteObject(db.Person_Work_Experience.First(c => c.ExpId == id && c.Person_Resume.ResumeId == resumeid));
            db.SaveChanges();
        }

        internal void DelResumeLanguage(int id, int resumeid)
        {
            db.DeleteObject(db.Person_Skill.First(c => c.SkillId == id && c.Person_Resume.ResumeId == resumeid));
            db.SaveChanges();
        }

        internal void DelPersonSearch(int id, int personid)
        {
            db.DeleteObject(db.Person_Search.First(c => c.SearchId == id && c.Person.PersonId == personid));
        }

        //public IList<Person_Resume> PersonSearch(string vocationId, string postClassId, string postType, string workAreaId, 
        //    int lastUpdate, int ageFrom, int ageTo, bool isPic, int sex, string education, int workLength)
        //{
        //    return db.pGetPerson(vocationId,postClassId,  postType,  workAreaId,  lastUpdate,  ageFrom,  ageTo,  isPic,  sex,  education,  workLength).ToList();
        //   // return db.pGetPerson("1","1","1","1",1,1,1,true,1,"1",1).ToList(); ;
        //}

        internal void AddResume(Person_Resume CurrentResume)
        {
            db.AddToPerson_Resume(CurrentResume);
            db.SaveChanges();
        }

        internal Person_Search GetPersonSearch(int id)
        {
            return db.Person_Search.First(c => c.SearchId == id);
        }

        public string SetResumeComplete(string complete, int index, int value)
        {
            var s = complete.Remove(index, 1);
            s = s.Insert(index, value.ToString());
            return s;
        }
    }
}
